#include "StdAfx.h"
#include "TestParticleEmitter2.h"


CTestParticleEmitter2::CTestParticleEmitter2(void)
	: CD3DParticleEmitter( 30, 10, CD3DParticleEmitter::PT_BillboardSprite )
{
}


CTestParticleEmitter2::~CTestParticleEmitter2(void)
{
}

void CTestParticleEmitter2::initProperties() {
//	reserve(1000);

	setPosition( 400, 300, 0 );
	setEmitPeriod( 0.05f );
	setLifeLimit( 3 );
	setNumToSpawn( 10 );
	setSize( 10 );
	reserve();

	HRESULT hr = D3DXCreateTextureFromFile( *g_pDevice, _T("../Resource/Arrow.png"), &m_pTex );
	assert( SUCCEEDED(hr) );
}

BOOL CTestParticleEmitter2::updateElement( TArray<ParticleElement>::Iter itElement, float fDeltaTime ) {
	
	D3DXROTATOR r( 0, 0, itElement->degDir );
	itElement->vPosition += r * itElement->vVelocity * fDeltaTime;

	return TRUE;
}

void CTestParticleEmitter2::initElement( ParticleElement* pElement ) {
	static int i=0;
	static float criteria = 0.f;
	static float criteriaDiff = 0.f;
	static int diffDirection = 1;
#ifdef _DEBUG
	static const D3DXCOLOR colorTable[10] = {
		D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1 ),
		D3DXCOLOR( 0.5f, 0.5f, 0.5f, 1 ),
		D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1 ),
		D3DXCOLOR( 0.0f, 1.0f, 1.0f, 1 ),
		D3DXCOLOR( 1.0f, 0.0f, 1.0f, 1 ),
		D3DXCOLOR( 1.0f, 0.0f, 0.0f, 1 ),
		D3DXCOLOR( 0.0f, 1.0f, 0.0f, 1 ),
		D3DXCOLOR( 0.5f, 0.5f, 1.0f, 1 ),
		D3DXCOLOR( 1.0f, 0.5f, 0.5f, 1 ),
		D3DXCOLOR( 0.5f, 1.0f, 1.0f, 1 )
	};
#endif

	float initDirection = criteria + 36.f * i;
	D3DXROTATOR r( 0, 0, initDirection );
	D3DXVECTOR3 v( 0, -50, 0 );

	pElement->bDead = false;
	pElement->fLife = m_fLifeLimit;
	pElement->vPosition = m_vPosition + (r * v);
#ifdef _DEBUG
	pElement->Color = colorTable[i];
#else
	pElement->Color = D3DXCOLOR( 1, 1, 1, 1 );
#endif
	pElement->fSize = m_fSize;
	pElement->vVelocity = D3DXVECTOR3(150, 0, 0);
	pElement->degDir = initDirection;

	if ( ++i >= 10 ) {
		i = 0;
	}
	
	criteria += criteriaDiff;
	if ( criteria > 360.f ) {
		criteria -= 360.f;
	}
	else if ( criteria < 0.f ) {
		criteria += 360.f;
	}

	static const float border = 2.f;
	if ( criteriaDiff > border ) {
		diffDirection = -1;
	}
	else if ( criteriaDiff < -border ) {
		diffDirection = 1;
	}
	criteriaDiff += 0.005f * diffDirection;
}